/**
 * 
 */
package org.t2.backstage.module.player;

import java.time.LocalDateTime;

import org.gaming.backstage.PageData;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.gaming.tool.ConsoleUtil;
import org.gaming.tool.DateTimeUtil;
import org.gaming.tool.GsonUtil;
import org.gaming.tool.LocalDateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.player.query.PlayerCommonQuery;
import org.t2.backstage.module.player.service.FirstPayRemainService;
import org.t2.backstage.module.player.service.PlayerLTVService;
import org.t2.backstage.module.player.service.PlayerPayInServerService;
import org.t2.backstage.module.player.service.PlayerPayRemainService;
import org.t2.backstage.module.player.service.PlayerRemainService;
import org.t2.backstage.module.player.service.PlayerSummaryService;
import org.t2.backstage.module.player.vo.FirstPayRemainVo;
import org.t2.backstage.module.player.vo.PlayerLtvVo;
import org.t2.backstage.module.player.vo.PlayerPayRemainVo;
import org.t2.backstage.module.player.vo.PlayerRemainVo;
import org.t2.backstage.module.player.vo.PlayerSummaryVo;

import jakarta.annotation.PostConstruct;

/**
 * @author YY
 */
@Controller
public class BuildTestLogController {

	@Autowired
	private PlayerRemainService playerRemainService;
	@Autowired
	private PlayerPayRemainService playerPayRemainService;
	@Autowired
	private PlayerLTVService playerLTVService;
	@Autowired
	private PlayerPayInServerService playerPayInServerService;
	@Autowired
	private PlayerSummaryService playerSummaryService;
	@Autowired
	private FirstPayRemainService firstPayRemainService;
	@Autowired
	private PlayerLTVController playerLTVController;
	@Autowired
	private PlayerRemainController playerRemainController;
	@Autowired
	private PlayerPayRemainController playerPayRemainController;
	@Autowired
	private FirstPayRemainController firstPayRemainController;
	@Autowired
	private PlayerSummaryController playerSummaryController;
	
	
	
	@PostConstruct
	private void console() {
		ConsoleUtil.addFunction("log", () -> {
			log();
		});
		ConsoleUtil.addFunction("logdata", () -> {
			logData();
		});
	}
	
	@ApiDocument("重算统计数据")
	@RequestMapping(value = "/buildTestLog/recount")
	@ResponseBody
	public String recount(@RequestParam("day") int day) {
		LocalDateTime time = LocalDateTime.now();
		long now = LocalDateTimeUtil.toEpochMilli(time);
		
		playerPayInServerService.countServerRecharge();
		
		for(int i = day; i >= 0; i--) {
			long countTime = now - DateTimeUtil.ONE_DAY_MILLIS * (i - 2);
			playerRemainService.countRemain(countTime);
			playerPayRemainService.countPayRemain(countTime);
			playerLTVService.countLTV(countTime);
			playerSummaryService.countSummary(countTime);
			firstPayRemainService.countPayRemain(countTime);
		}
		return "ok";
	}
	
	public void log() {
		
		LocalDateTime time = LocalDateTimeUtil.now().minusDays(1);
		long now = LocalDateTimeUtil.toEpochMilli(time);
		
		playerPayInServerService.countServerRecharge();
		
		for(int i = 0; i < 7; i++) {
			long countTime = now + DateTimeUtil.ONE_DAY_MILLIS * i;
			System.out.println("统计时间：" + DateTimeUtil.formatMillis(DateTimeUtil.YYYY_MM_DD_HH_MM_SS, countTime));
			playerRemainService.countRemain(countTime);
			playerPayRemainService.countPayRemain(countTime);
			playerLTVService.countLTV(countTime);
			playerSummaryService.countSummary(countTime);
			firstPayRemainService.countPayRemain(countTime);
		}
	}
	
	public void logData() {
		PlayerCommonQuery query = new PlayerCommonQuery();
		System.out.println("===============ltv================");
		PageData<PlayerLtvVo> ltvData = playerLTVController.data(query);
		for(PlayerLtvVo vo : ltvData.getData()) {
			System.out.println(GsonUtil.toJson(vo));
		}
		
		System.out.println("===============留存================");
		PageData<PlayerRemainVo> remainData = playerRemainController.data(query);
		for(PlayerRemainVo vo : remainData.getData()) {
			System.out.println(GsonUtil.toJson(vo));
		}
		
		System.out.println("===============付费留存================");
		PageData<PlayerPayRemainVo> payRemainData = playerPayRemainController.data(query);
		for(PlayerPayRemainVo vo : payRemainData.getData()) {
			System.out.println(GsonUtil.toJson(vo));
		}
		
		System.out.println("===============首冲留存================");
		PageData<FirstPayRemainVo> firstPayRemainData = firstPayRemainController.data(query);
		for(FirstPayRemainVo vo : firstPayRemainData.getData()) {
			System.out.println(GsonUtil.toJson(vo));
		}
		
		System.out.println("===============统计================");
		PageData<PlayerSummaryVo> summaryData = playerSummaryController.data(query);
		for(PlayerSummaryVo vo : summaryData.getData()) {
			System.out.println(GsonUtil.toJson(vo));
		}
		
		System.out.println("===============================");
	}
}
